package com.bgs.controller;

import com.bgs.client.OrderDetailClient;
import com.bgs.client.OrderStoreClient;
import com.bgs.common.R;
import com.bgs.pojo.Order;
import com.bgs.pojo.vo.CircleTow;
import com.bgs.pojo.vo.OrderDetail;
import com.bgs.pojo.vo.OrderTow;
import com.bgs.pojo.vo.Store;
import com.bgs.pojo.vo.OneOrderAndDetail;
import com.bgs.pojo.Order;
import com.bgs.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.*;
import com.bgs.service.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.Date;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 订单 前端控制器
 * </p>
 *
 * @author Asia
 * @since 2021-09-14
 */
@Api(value = "订单模块")
@RestController
@RequestMapping("/bgs/order")
public class OrderController {
    @Autowired
    private OrderService orderService;
    @Autowired
    private OrderDetailClient orderDetailClient;
    @Autowired
    private OrderStoreClient orderStoreClient;
    /**
     * 查看用户购买商品订单
     * @param beforeUserId
     * @return
     */
    @GetMapping("/TowGetOrder")
    private R TowGetOrder(Integer beforeUserId){
        //根据用户id查看中间表
        List<OrderTow> list = orderService.TowGetOrder(beforeUserId);
        list.stream().forEach(x->{
            //循环得到中间表中商品的id
            List<OrderDetail> list1 = orderDetailClient.TowgetOrderDetail(x.getOrderMiddleDetail().getOrderDetailId());
            list1.stream().forEach(y->{
                //根据循环得到的商品中店铺的id查询店铺信息
                List<Store> list2 = orderStoreClient.TowOrderStore(y.getStoreId());
                y.setStores(list2);
            });
            x.setOrderDetails(list1);
        });
        return R.ok().data("list",list);
    }

    @GetMapping("/TowTuiOrder")
    public R TowTuiOrder(Integer beforeUserId){
        //根据用户id查看中间表
        List<OrderTow> list = orderService.TowTuiOrder(beforeUserId);
        list.stream().forEach(x->{
            //循环得到中间表中商品的id
            List<OrderDetail> list1 = orderDetailClient.TowgetOrderDetail(x.getOrderMiddleDetail().getOrderDetailId());
            list1.stream().forEach(y->{
                //根据循环得到的商品中店铺的id查询店铺信息
                List<Store> list2 = orderStoreClient.TowOrderStore(y.getStoreId());
                y.setStores(list2);
            });
            x.setOrderDetails(list1);
        });
        return R.ok().data("list",list);
    }

    /**
     * 用户进行退货
     * @param beforeUserId
     * @return
     */
    @GetMapping("/TowOrderUp")
    public R TowOrderUp(Integer beforeUserId){
        int i = orderService.TowOrderUp(beforeUserId);
        return R.ok().data("i",i);
    }

    @ApiOperation(value = "根据用户名的id查询用户的全部订单")
    @GetMapping("OneOrderrUserId/{beforeUserId}")
    public R OneOrderrUserId(@PathVariable Integer beforeUserId){
        return  orderService.OneOrderrUserId(beforeUserId);
    }
    @ApiOperation(value = "根据用户名的id查询用户的订单状态0待付款 ")
    @GetMapping("OneOrderrUserIdByStatus0/{beforeUserId}")
    public R OneOrderrUserIdByStatus0(@PathVariable Integer beforeUserId){
        return  orderService.OneOrderrUserIdByStatus0(beforeUserId);
    }
    @ApiOperation(value = "根据用户名的id查询用户的订单状态1待发货 ")
    @GetMapping("OneOrderrUserIdByStatus1/{beforeUserId}")
    public R OneOrderrUserIdByStatus1(@PathVariable Integer beforeUserId){
        return  orderService.OneOrderrUserIdByStatus1(beforeUserId);
    }
    @ApiOperation(value = "根据用户名的id查询用户的订单状态2待收货 ")
    @GetMapping("OneOrderrUserIdByStatus2/{beforeUserId}")
    public R OneOrderrUserIdByStatus2(@PathVariable Integer beforeUserId){
        return  orderService.OneOrderrUserIdByStatus2(beforeUserId);
    }
    @ApiOperation(value = "根据用户名的id查询用户的订单状态3待评价 ")
    @GetMapping("OneOrderrUserIdByStatus3/{beforeUserId}")
    public R OneOrderrUserIdByStatus3(@PathVariable Integer beforeUserId){
        return  orderService.OneOrderrUserIdByStatus3(beforeUserId);
    }
    @ApiOperation(value = "根据用户名的id查询用户的订单状态4退款/售后 ")
    @GetMapping("OneOrderrUserIdByStatus4/{beforeUserId}")
    public R OneOrderrUserIdByStatus4(@PathVariable Integer beforeUserId){
        return  orderService.OneOrderrUserIdByStatus4(beforeUserId);
    }
    @ApiOperation(value = "根据用户名的id查询用户的订单状态5交易关闭")
    @GetMapping("OneOrderrUserIdByStatus5/{beforeUserId}")
    public R OneOrderrUserIdByStatus5(@PathVariable Integer beforeUserId){
        return  orderService.OneOrderrUserIdByStatus5(beforeUserId);
    }


    @ApiOperation(value = "点击确认收货3修改状态为3待评价")
    @PostMapping("OneOrderUpdateIdByStatus/{orderId}")
    public R OneOrderUpdateIdByStatus(@PathVariable Integer orderId){

         boolean b  = orderService.updateByOrderId(orderId);
        if (b) {
            return R.ok();
        }else {
            return R.error();
        }

    }



    /*查询订单 生成eachars*/
    @GetMapping("/fourQueryOrder")
    private R fourQueryOrder(Integer id) {
        return orderService.fourQueryOrder(id);
    }

    @PostMapping("/queenOrder")
    private R queenOrder(@RequestBody Map map) {

        return orderService.queenOrder(map);
    }
}

